package com.tianqu.tms.www.member.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.action.MemberUDTopListAction;
import com.tianqu.tms.www.member.actionform.MemberInputActionForm;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
import com.tianqu.tms.www.member.viewhelper.MemberUDViewHelper;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;

/**
 * 会员信息修改删除列表页Controller
 * 
 * @author tianshifu
 * 
 */
public class MemberUDTopListController extends TmsController {

	private static final String MEMLIST = "memList";

	private Logger logger = Logger.getLogger(MemberUDTopListController.class);

	public static final String PAGING = "paging";

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			Member entity = Member.getInstance();

			if (uc.getAttribute(MemberUDTopListAction.PAGENO) != null) {
				entity.setPageNo(Integer.parseInt((String) uc
						.getAttribute(MemberUDTopListAction.PAGENO)));
			}
			if (uc.getAttribute(MemberUDTopListAction.MAXINDEX) != null) {
				entity.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(MemberUDTopListAction.MAXINDEX)));
			}
			if (uc.getAttribute(MemberUDTopListAction.SORTKEY) != null) {
				entity.setSortKey(Integer.parseInt((String) uc
						.getAttribute(MemberUDTopListAction.SORTKEY)));
			}
			if (uc.getAttribute(MemberUDTopListAction.ASC) != null) {
				entity.setAsc((new Boolean((String) uc
						.getAttribute(MemberUDTopListAction.ASC)))
						.booleanValue());
			}
			
			MemberInputActionForm memberForm = (MemberInputActionForm)uc.getInputForm();
			// 判定翻页
			if (uc.getAttribute("seniorForm") != null) {
				memberForm = (MemberInputActionForm) uc.getAttribute("seniorForm");
			}
			// 从member表中的到一个处理好的list直接打到前台
			List<Member> list = entity.findAllMemberInfo(memberForm.getMemberId(),memberForm.getMemberStatus());

			MemberInputViewHelper view = new MemberInputViewHelper();

			view.setMemList(list);
			
			uc.removeAttribute(MEMLIST);
			uc.setAttribute(MEMLIST, view);
			uc.setAttribute(PAGING, entity);
			uc.setAttribute("seniorForm", memberForm);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
